{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------\n",
    "时间：2022/10/24  week10\n",
    "----------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    ">1. while循环\n",
    ">2. 购物车实践"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.while循环\n",
    "* 回顾： for循环:  for + range()  可以指定循环的次数------>关于网页url队列的抓取实践\n",
    "* while循环：当某种条件成立时，停止循环\n",
    "* while True:无限循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙 0\n",
      "你是风儿我是沙 1\n",
      "你是风儿我是沙 2\n",
      "你是风儿我是沙 3\n",
      "你是风儿我是沙 4\n",
      "你是风儿我是沙 5\n",
      "你是风儿我是沙 6\n",
      "你是风儿我是沙 7\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙\",count)\n",
    "    count = count+1\n",
    "    if count == 8:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：huang\n",
      "密码：jun\n",
      "账号或密码不正确，您还有2次尝试机会\n",
      "账号：huiabk\n",
      "密码：anfnoi\n",
      "账号或密码不正确，您还有1次尝试机会\n",
      "账号：fauihn\n",
      "密码：feeee\n",
      "一小时后再试\n"
     ]
    }
   ],
   "source": [
    "# 2. while True循环，可以让用户重复输入\n",
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "count = 0   #计数器\n",
    "while True:\n",
    "    username = input(\"账号：\")\n",
    "    password = input(\"密码：\")\n",
    "    if user01 == username and  password01 == password:\n",
    "        print(\"登录成功，欢迎\"+user01)\n",
    "        break   #中止循环\n",
    "    else:\n",
    "        if count<2:\n",
    "            print(\"账号或密码不正确，您还有\"+str(2-count)+\"次尝试机会\")\n",
    "        else:\n",
    "            print(\"一小时后再试\")\n",
    "            break\n",
    "        count = count+1    \n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 购物车项目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">要求：\n",
    ">>1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    ">>2. 让用户输入商品编号进行商品的购买。\n",
    ">>3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    ">>4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：160\n"
     ]
    }
   ],
   "source": [
    "# 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "product_list = [\n",
    "    [\"柠蜜雪檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8],\n",
    "]\n",
    "print(\"---------商品列表---------\")\n",
    "print(product_list[0][0],str(product_list[0][1])+\"元\")      #将print进行格式化输出\n",
    "print(product_list[1][0],str(product_list[1][1])+\"元\")\n",
    "print(product_list[2][0],str(product_list[2][1])+\"元\")\n",
    "print(product_list[3][0],str(product_list[3][1])+\"元\")\n",
    "Alipay = input(\"请输入余额：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['柠蜜雪檬水', 6], ['蜜桃四季春', 10], ['芒果奶盖', 12], ['葡萄波波奶茶', 8]]\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"柠蜜雪檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8],\n",
    "]\n",
    "print(product_list)      #将print进行格式化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：234\n",
      "---------商品列表---------\n",
      "柠蜜雪檬水 6元\n",
      "蜜桃四季春 10元\n",
      "芒果奶盖 12元\n",
      "葡萄波波奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "# 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "product_list = [\n",
    "    [\"柠蜜雪檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8],\n",
    "]\n",
    "Alipay = input(\"请输入余额：\")\n",
    "print(\"---------商品列表---------\")\n",
    "for i in range(4):\n",
    "    print(product_list[i][0],str(product_list[i][1])+\"元\")      #将print进行格式化输出\n",
    "#print(product_list[1][0],str(product_list[1][1])+\"元\")\n",
    "#print(product_list[2][0],str(product_list[2][1])+\"元\")\n",
    "#print(product_list[3][0],str(product_list[3][1])+\"元\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 2. 让用户输入商品编号进行商品的购买。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：678\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "# 添加商品编号\n",
    "\n",
    "product_list = [\n",
    "    [\"柠蜜雪檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8],\n",
    "]\n",
    "Alipay = input(\"请输入余额：\")\n",
    "print(\"---------商品列表---------\")\n",
    "for i in range(4):\n",
    "    print(i,product_list[i][0],str(product_list[i][1])+\"元\")   # 添加商品编号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：23\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n",
      "请输入商品编号0\n",
      "商品购买成功，正在制作中...\n"
     ]
    }
   ],
   "source": [
    "# 添加商品编号\n",
    "\n",
    "product_list = [\n",
    "    [\"柠蜜雪檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8],\n",
    "]\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "print(\"---------商品列表---------\")\n",
    "for i in range(4):\n",
    "    print(i,product_list[i][0],str(product_list[i][1])+\"元\")   # 添加商品编号\n",
    "# 2.让用户输入商品编号进行商品的购买\n",
    "\n",
    "user_choice = input(\"请输入商品编号\")\n",
    "# 情景一：余额大于商品价格\n",
    "if Alipay >= product_list[int(user_choice)][1]:\n",
    "    print(\"商品购买成功，正在制作中...\")\n",
    "# 情景二:余额小于商品价格\n",
    "else:\n",
    "    print(\"您的余额不足，请更换支付方式\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设用户选择了0\n",
    "product_list[0][1]\n",
    "# 用户选择的商品价格："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：50\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n",
      "请输入商品编号2\n",
      "商品购买成功，正在制作中...您的余额还有38元\n"
     ]
    }
   ],
   "source": [
    "# 账户余额变化\n",
    "\n",
    "product_list = [\n",
    "    [\"柠蜜雪檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8],\n",
    "]\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "print(\"---------商品列表---------\")\n",
    "for i in range(4):\n",
    "    print(i,product_list[i][0],str(product_list[i][1])+\"元\")   # 添加商品编号\n",
    "# 2.让用户输入商品编号进行商品的购买\n",
    "\n",
    "user_choice = input(\"请输入商品编号\")\n",
    "# 情景一：余额大于商品价格\n",
    "if Alipay >= product_list[int(user_choice)][1]:\n",
    "    Alipay = Alipay - product_list[int(user_choice)][1]     #余额的价格变化\n",
    "    print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(Alipay))\n",
    "# 情景二:余额小于商品价格\n",
    "else:\n",
    "    print(\"您的余额不足，请更换支付方式\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：23\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n",
      "请输入商品编号2\n",
      "商品购买成功，正在制作中...您的余额还有11元\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n",
      "请输入商品编号2\n",
      "您的余额不足，请更换支付方式\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "# 添加买多次的功能\n",
    "\n",
    "product_list = [\n",
    "    [\"柠蜜雪檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8],\n",
    "]\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"---------商品列表---------\")\n",
    "    for i in range(4):\n",
    "        print(i,product_list[i][0],str(product_list[i][1])+\"元\")   # 添加商品编号\n",
    "    # 2.让用户输入商品编号进行商品的购买\n",
    "    user_choice = input(\"请输入商品编号\")\n",
    "    # 情景一：余额大于商品价格\n",
    "    if Alipay >= product_list[int(user_choice)][1]:\n",
    "        Alipay = Alipay - product_list[int(user_choice)][1]     #余额的价格变化\n",
    "        print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(Alipay))\n",
    "    # 情景二:余额小于商品价格\n",
    "    else:\n",
    "        print(\"您的余额不足，请更换支付方式\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：15\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n",
      "请输入商品编号2\n",
      "商品购买成功，正在制作中...您的余额还有3元\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n",
      "请输入商品编号1\n",
      "您的余额不足，请更换支付方式\n",
      "---------商品列表---------\n",
      "0 柠蜜雪檬水 6元\n",
      "1 蜜桃四季春 10元\n",
      "2 芒果奶盖 12元\n",
      "3 葡萄波波奶茶 8元\n",
      "请输入商品编号q\n",
      "您已经购买的商品： [['芒果奶盖', 12]]\n",
      "您的余额还有3，欢迎下次光临\n"
     ]
    }
   ],
   "source": [
    "# 添加退出功能，购物车\n",
    "\n",
    "product_list = [\n",
    "    [\"柠蜜雪檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8],\n",
    "]\n",
    "shopping_car = []  # 添加购物车\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"---------商品列表---------\")\n",
    "    for i in range(4):\n",
    "        print(i,product_list[i][0],str(product_list[i][1])+\"元\")   # 添加商品编号\n",
    "    # 2.让用户输入商品编号进行商品的购买\n",
    "    user_choice = input(\"请输入商品编号\")\n",
    "    # 情景01 用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if Alipay >= product_list[int(user_choice)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_choice)][1]     #余额的价格变化\n",
    "            shopping_car.append(product_list[int(user_choice)])   #将用户选择的商品打印为购物车列表\n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(Alipay))\n",
    "        # 情景二:余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式\")\n",
    "    # 情景02 用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已经购买的商品：\",shopping_car)\n",
    "        print(\"您的余额还有%s，欢迎下次光临\"%(Alipay))\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
